import sys
import keyboard
import win32api
import win32con
import time
from pynput import mouse
import ctypes  # 用于DPI感知
from winotify import Notification

begin=Notification(app_id="auto_clicker",title="app start",msg="程序启动，请点击屏幕任意位置开始点击，单击空格停止程序",duration="short")
begin.show()
try:
    # 尝试设置为 PER_MONITOR_DPI_AWARE (2)，让程序识别屏幕缩放
    ctypes.windll.shcore.SetProcessDpiAwareness(2)
except Exception as e:
    print("某些系统版本可能不支持 SetProcessDpiAwareness:", e)
    ctypes.windll.user32.SetProcessDPIAware()

xx = 0
yy = 0

def mouse_click(x, y):
    # 移动鼠标到指定位置
    win32api.SetCursorPos((x, y))
    # 模拟鼠标按下和释放
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)
    time.sleep(0.03)  # 保持按下的时间也可以稍作调整
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0)

def on_click(x, y, button, pressed):
    global xx, yy
    if pressed:
        xx = x
        yy = y
        print(f"捕获到的坐标: ({x}, {y})")  # 打印捕获的坐标用于调试
        start = Notification(app_id="auto_clicker", title="start click", msg=f"捕获到坐标{x}:{y}，开始点击",duration="short")
        start.show()

        # 返回 False 以停止监听器
        return False
    return True

# 首次获取坐标
with mouse.Listener(on_click=on_click) as listener:
    listener.join()


# 使用示例
while True:
    mouse_click(xx, yy)
    if keyboard.is_pressed("space"):
        end= Notification(app_id="auto_clicker", title="stop click", msg=f"程序退出")
        end.show()
        sys.exit(0)
    time.sleep(0.02)  # 适当增加循环延迟